package com.ffcs.crmd.tsp.admin.service.impl;

import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ffcs.crmd.tsp.admin.entity.JobDetailParam;
import com.ffcs.crmd.tsp.admin.service.IJobDetailParamService;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailParam;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.core.util.BeanUtils;

/**
 * 
 * 功能说明:子任务参数service
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年4月26日 下午5:02:46
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
@Service
public class JobDetailParamServiceImpl extends AbstractService implements IJobDetailParamService{
    
    @Override
    public JobDetailParam get(JobDetailParam jobDetailParam) throws TspException {
        try {
            TspJobDetailParam tspJobDetailParamVO = new TspJobDetailParam();
            BeanUtils.applyIf(tspJobDetailParamVO, jobDetailParam, false);
            
            TspClient tspClient = getClient(jobDetailParam.getClusterName());
            TspResult<TspJobDetailParam> tspResult = tspClient.queryJobDetailParam(tspJobDetailParamVO);
            validateResult(tspResult);
            
            JobDetailParam rjobDetailParam = new JobDetailParam();
            BeanUtils.applyIf(rjobDetailParam, tspResult.getRetVo().getObject(), false);
            return rjobDetailParam;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public PageInfo<JobDetailParam> query(JobDetailParam jobDetailParam, int pageNumber, int pageSize) throws TspException {
        try {
            TspJobDetailParam tspJobDetailParamVO = new TspJobDetailParam();
            BeanUtils.applyIf(tspJobDetailParamVO, jobDetailParam, false);
            tspJobDetailParamVO.setPageNumber(pageNumber);
            tspJobDetailParamVO.setPageSize(pageSize);
            
            TspClient tspClient = getClient(jobDetailParam.getClusterName());
            TspResult<TspJobDetailParam> tspResult = tspClient.queryJobDetailParam(tspJobDetailParamVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspJobDetailParam> tspJobDetailParamPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<JobDetailParam> jobDetailParamPageInfo = new PageInfo<JobDetailParam>();
            BeanUtils.applyIf(jobDetailParamPageInfo, tspJobDetailParamPageInfo, false);
            jobDetailParamPageInfo.setList(BeanUtils.copyList(tspJobDetailParamPageInfo.getList(), JobDetailParam.class));
            return jobDetailParamPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void save(JobDetailParam jobDetailParam) throws TspException {
        try {
            TspJobDetailParam tspJobDetailParamVO = new TspJobDetailParam();
            BeanUtils.applyIf(tspJobDetailParamVO, jobDetailParam);
            TspClient tspClient = getClient(jobDetailParam.getClusterName());
            TspResult<?> tspResult = tspClient.createJobDetailParam(tspJobDetailParamVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void delete(JobDetailParam jobDetailParam) throws TspException {
        try {
            TspJobDetailParam tspJobDetailParamVO = new TspJobDetailParam();
            tspJobDetailParamVO.setId(jobDetailParam.getId());
            tspJobDetailParamVO.setClusterName(jobDetailParam.getClusterName());
            
            TspClient tspClient = getClient(jobDetailParam.getClusterName());
            TspResult<?> tspResult = tspClient.deleteJobDetailParam(tspJobDetailParamVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void update(JobDetailParam jobDetailParam) throws TspException {
        try {
            TspJobDetailParam tspJobDetailParamVO = new TspJobDetailParam();
            BeanUtils.applyIf(tspJobDetailParamVO, jobDetailParam);
            TspClient tspClient = getClient(jobDetailParam.getClusterName());
            TspResult<?> tspResult = tspClient.updateJobDetailParam(tspJobDetailParamVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
}
